cmake_minimum_required(VERSION 3.7)
project(jakaserver)

set(GRPCPROTO jaka.proto)
set(PROTO_PY_FILE 
${CMAKE_CURRENT_LIST_DIR}/scripts/jaka_pb2_grpc.py 
${CMAKE_CURRENT_LIST_DIR}/scripts/jaka_pb2.py
)

set(JAKA_SERVER_PYC 
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_server.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_check.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_module.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_grpc_server.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_tcp_server.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/brd.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ftpserver.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/ftptransfer.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/downgrade_handle.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tcp_command_process.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/tool_function.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_pb2_grpc.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_pb2.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_grpc_def.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_exception_code.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_controller_client.pyc
)

set(ADDON_PATH ${CMAKE_CURRENT_LIST_DIR}/addon_manager)

add_custom_target(jaka_server_package ALL DEPENDS ${JAKA_SERVER_PYC} errorcode ${ADDON_MGR_PYC} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/start_addon_manager)
add_dependencies(jaka_server_package pylog web_socket_io jaka_upgrade)

add_custom_command(OUTPUT ${JAKA_SERVER_PYC}
    COMMAND cp ${ADDON_PATH}/addon_grpc_def.py ${ADDON_PATH}/addon_exception_code.py ${CMAKE_CURRENT_LIST_DIR}/scripts
    # Search Keyword: PEP 3147
    COMMAND python3 -m compileall -b .
    COMMAND sleep 1
    COMMAND mv *.pyc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    # COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/rename_to_pyc.py -d ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    DEPENDS ${CMAKE_CURRENT_LIST_DIR}/scripts/*.py ${PROTO_PY_FILE} ${ADDON_PATH}/addon_grpc_def.py ${ADDON_PATH}/addon_exception_code.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/scripts
)

add_custom_command(OUTPUT ${PROTO_PY_FILE}    
    COMMAND ${JKZUC_ROOT_DIR}/../.venv/bin/python -m grpc_tools.protoc -I./proto  --python_out=./scripts  --grpc_python_out=./scripts  ${GRPCPROTO}
	COMMAND sleep 1
    DEPENDS ./proto/${GRPCPROTO}
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
)

install(FILES 
${JAKA_SERVER_PYC} 
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_grpc_def.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_exception_code.pyc
DESTINATION python3
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
)

add_custom_command(OUTPUT errorcode
    COMMAND rm -rf ${JKZUC_ROOT_DIR}/scripts/log/events.rules
    COMMAND rm -rf ${JKZUC_ROOT_DIR}/docs/ecode
    COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/error_code/ecode2lan.py
    COMMAND tar -zcvf ${JKZUC_ROOT_DIR}/scripts/log/events.rules/zh.tar.gz -C ${JKZUC_ROOT_DIR}/scripts/log/events.rules/ zh.json
    COMMAND tar -zcvf ${JKZUC_ROOT_DIR}/scripts/log/events.rules/en.tar.gz -C ${JKZUC_ROOT_DIR}/scripts/log/events.rules/ en.json
    COMMAND tar -zcvf ${JKZUC_ROOT_DIR}/scripts/log/events.rules/ja.tar.gz -C ${JKZUC_ROOT_DIR}/scripts/log/events.rules/ ja.json
    COMMAND tar -zcvf ${JKZUC_ROOT_DIR}/scripts/log/events.rules/vi.tar.gz -C ${JKZUC_ROOT_DIR}/scripts/log/events.rules/ vi.json
    COMMAND tar -zcvf ${JKZUC_ROOT_DIR}/scripts/log/events.rules/fr.tar.gz -C ${JKZUC_ROOT_DIR}/scripts/log/events.rules/ fr.json
    WORKING_DIRECTORY ${JKZUC_ROOT_DIR}
)

set(ADDON_MGR_PYC
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_config_parser.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_eventqueue.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_grpc_def.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_exception_code.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_port_mgr.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_venvwrapper.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_config.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_info.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_requirement_mgr.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_whitelist.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_controller_server.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_exception.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_installer.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_status.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_converter.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_executor.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_logger.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/addon_utils.pyc
)

add_custom_command(OUTPUT ${ADDON_MGR_PYC} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/start_addon_manager
    # Search Keyword: PEP 3147
    COMMAND python3 -m compileall -b .
    COMMAND sleep 1
    COMMAND mv start_addon_manager*.pyc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/start_addon_manager
    COMMAND chmod +x ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/start_addon_manager
    COMMAND mv *.pyc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    DEPENDS ${CMAKE_CURRENT_LIST_DIR}/addon_manager/*.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/addon_manager
)

set(PYLOG_PYC
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pylog_handler.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pylog.pyc
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/pylog_formatter.pyc
)

add_custom_target(pylog DEPENDS ${PYLOG_PYC})

add_custom_command(OUTPUT ${PYLOG_PYC}
    # Search Keyword: PEP 3147
    COMMAND python3 -m compileall -b .
    COMMAND sleep 1
    COMMAND mv *.pyc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    DEPENDS ${CMAKE_CURRENT_LIST_DIR}/pylog/*.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/pylog
)

install(FILES 
${PYLOG_PYC} 
DESTINATION python3
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
)

install(PROGRAMS 
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/start_addon_manager
DESTINATION bin
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
)

set(WEB_SOCKET_IO_PYC
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/wsi.pyc
)

add_custom_target(web_socket_io DEPENDS ${WEB_SOCKET_IO_PYC})

add_custom_command(OUTPUT ${WEB_SOCKET_IO_PYC}
    COMMAND python3 -m compileall -b .
    COMMAND sleep 1
    # 替换后缀为cpython-39.pyc文件为.pyc的文件，以便于python能够正常导入
    # COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/rename_to_pyc.py -d __pycache__
    COMMAND mv wsi.pyc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    DEPENDS ${CMAKE_CURRENT_LIST_DIR}/web_socket_io/*.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/web_socket_io
)

install(FILES 
${WEB_SOCKET_IO_PYC} 
DESTINATION python3
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
)

set(JAKA_UPGRADE_PYC
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_upgrade_loader.pyc
# ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/jaka_upgrade.pyc
)

add_custom_target(jaka_upgrade DEPENDS ${JAKA_UPGRADE_PYC})

add_custom_command(OUTPUT ${JAKA_UPGRADE_PYC}
    COMMAND python3 -m compileall -b .
    COMMAND sleep 1
    COMMAND mv *.pyc ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
    DEPENDS ${CMAKE_CURRENT_LIST_DIR}/upgrade/*.py
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/upgrade
)

install(PROGRAMS 
${JAKA_UPGRADE_PYC}
DESTINATION bin
PERMISSIONS OWNER_READ OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
)